using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Pervote.Core;

public class Cloud
{
    string cloudView = "";

    public Cloud(List<Poll> polls)
    {
        //Fields in da table
        BuildCloudView(polls);
    }

    private void BuildCloudView(List<Poll> polls)
    {
        long maxVotes = 0;
        long votes = 0;
        foreach (Poll poll in polls)
        {
            votes = poll.TotalVotes;
            if (votes > maxVotes) maxVotes = votes;
        }

        // we have to divide maxVotes into 10 parts, logarithmically!
        // ie x^10 = maxVotes (or 10 log x = log maxVotes)
        // x = e^((log maxVotes)/10)
        double logX = Math.Log(maxVotes) / 10;
        double x = Math.Exp(logX);

        double[] xTable = new double[10];
        //build reference table
        //everything > x ^ 9 will fall into the last bracket. Since X ^ 10 is the maximum
        for (int i = 1; i < 10; i++)
        {
            xTable[i] = Math.Pow(x, i);
        }

        int itemDisplaySize = 0;
        foreach (Poll poll in polls)
        {
            long pollVotes = poll.TotalVotes;
            for (int i = 1; i < 10; i++)
            {
                itemDisplaySize = i;
                if (pollVotes < xTable[i]) break;
            }

            if (cloudView != "") cloudView += " / ";
            cloudView += "<a href=\"/PollMain.aspx?id=" + poll.Id.ToString().ToString() + "\"";
            cloudView += " class=\"" + "linkSize-" + itemDisplaySize.ToString() + "\"";
            cloudView += ">";
            cloudView += poll.Title;
            cloudView += "</a>\n";
        }
    }

    public string GetCloudView()
    {
        return cloudView;
    }
}
